package com.ctsi.ssdc.service.impl;


import com.ctsi.ssdc.util.GrouponConstant;
import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import com.ctsi.ssdc.domain.LitemallGrouponRules;
import com.ctsi.ssdc.domain.LitemallGrouponRulesExample;
import com.ctsi.ssdc.service.LitemallGrouponRulesService;
import com.ctsi.ssdc.repository.LitemallGrouponRulesRepository;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.Objects;
import com.ctsi.ssdc.service.impl.StrengthenBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctsi.ssdc.model.PageResult;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.PageHelper;
/**
 * Service Implementation for managing LitemallGrouponRules.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallGrouponRulesServiceImpl 
	extends StrengthenBaseServiceImpl<LitemallGrouponRulesRepository, LitemallGrouponRules, Integer, LitemallGrouponRulesExample> 
	implements LitemallGrouponRulesService {

    @Autowired
    LitemallGrouponRulesRepository litemallGrouponRulesRepository;


    /**
    * GET  /litemallGrouponRuless : get the litemallGrouponRuless firstStringBaseColumn.
    */
    @Override
    public PageResult<LitemallGrouponRules> findFirstStringColumn(String goodsName,Pageable pageable){
        String str = goodsName;
        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }
        LitemallGrouponRulesExample litemallGrouponRulesExample = new LitemallGrouponRulesExample();
        String orderBy = getPageOrderBy(pageable);
        if(StringUtils.isNotEmpty(orderBy)) {
            litemallGrouponRulesExample.setOrderByClause(orderBy);
        }
        if (StringUtils.isEmpty(str)){
            litemallGrouponRulesExample.createCriteria();
        } else{
            litemallGrouponRulesExample.createCriteria().andGoodsNameLike("%" + str +"%");
        }
        List<LitemallGrouponRules>  data = litemallGrouponRulesRepository.selectByExample(litemallGrouponRulesExample);

        long count = 0L;
        if (CollectionUtils.isNotEmpty(data)){
            count = litemallGrouponRulesRepository.countByExample(litemallGrouponRulesExample);
        }
        return new PageResult<LitemallGrouponRules>(data,count,count);
    }

    @Override
    public LitemallGrouponRules findById(Integer grouponRulesId) {
        return litemallGrouponRulesRepository.selectByPrimaryKey(grouponRulesId);
    }

    @Override
    public List<LitemallGrouponRules>  queryList(Integer page, Integer size, String sort, String order) {
        LitemallGrouponRulesExample example = new LitemallGrouponRulesExample();
        example.or().andStatusEqualTo(GrouponConstant.RULE_STATUS_ON).andDeletedEqualTo(false);
        example.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, size);
        return litemallGrouponRulesRepository.selectByExample(example);
    }

    @Override
    public List<LitemallGrouponRules> queryList(Integer page, Integer size) {
        return queryList(page, size, "add_time", "desc");
    }

    @Override
    public List<LitemallGrouponRules> queryByGoodsId(Integer goodsId) {
        LitemallGrouponRulesExample example = new LitemallGrouponRulesExample();
        example.or().andGoodsIdEqualTo(goodsId).andStatusEqualTo(GrouponConstant.RULE_STATUS_ON).andDeletedEqualTo(false);
        return litemallGrouponRulesRepository.selectByExample(example);
    }

    private String getPageOrderBy(Pageable page) {
        if(page!= null && page.getSort() != null) {
            StringBuilder sb = new StringBuilder();
            page.getSort().forEach(sort -> sb.append(sort.getProperty())
            .append(" ").append(sort.getDirection()).append(","));
            if(sb.length() > 1) {
                return (sb.substring(0,sb.length()-1));
             }
        }
        return null;
    }
}
